package com.hsgene.common.util.result;

import com.hsgene.common.util.exception.ErrorCode;

import java.io.Serializable;
import java.util.List;

/**
 * 接口调用返回结果
 *
 * @author wangbing
 * @version 1.0, 2018/6/22
 */
public class DoctorApiResult<T> implements Serializable {

    private static final long serialVersionUID = -1819003631683535598L;

    /**
     * 接口调用成功状态码
     */
    public final static int SUCCESS_CODE = 200;

    /**
     * 接口调用成功message消息
     */
    public final static String SUCCESS_MESSAGE = "OK!";

    /**
     * 状态码：200表示正常，其它表示错误码
     */
    private int status;

    /**
     * 成功／失败信息
     */
    private String message;

    /**
     * 业务数据：单个结果
     */
    private T info;

    /**
     * 业务数据：多个结果
     */
    private List<T> list;

    public DoctorApiResult() {
    }

    private DoctorApiResult(int status, String message) {
        this.status = status;
        this.message = message;
    }

    private DoctorApiResult(int status, String message, T info) {
        this.status = status;
        this.message = message;
        this.info = info;
    }

    private DoctorApiResult(int status, String message, List<T> list) {
        this.status = status;
        this.message = message;
        this.list = list;
    }

    /**
     * 构建默认成功结果
     *
     * @param <T>
     * @return
     */
    public static <T> DoctorApiResult<T> defaultSuccess() {
        return new DoctorApiResult<>(SUCCESS_CODE, SUCCESS_MESSAGE);
    }

    /**
     * 构建默认失败结果
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> DoctorApiResult<T> defaultFail(ErrorCode errorCode) {
        return new DoctorApiResult<>(errorCode.getCode(), errorCode.getMessage());
    }

    /**
     * 从ApiResult中转化返回单个结果
     *
     * @param apiResult
     * @param <T>
     * @return
     */
    public static <T> DoctorApiResult<T> from(ApiResult<T> apiResult) {
        if (apiResult.isSucc()) {
            return new DoctorApiResult<>(SUCCESS_CODE, SUCCESS_MESSAGE, apiResult.getData());
        }
        return new DoctorApiResult<>(apiResult.getCode(), apiResult.getMessage(), apiResult.getData());
    }

    /**
     * 从ApiResult中转化返回列表结果
     *
     * @param apiResult
     * @param list
     * @param <T>
     * @return
     */
    public static <T> DoctorApiResult<T> from(ApiResult<List<T>> apiResult, List<T> list) {
        if (apiResult.isSucc()) {
            return new DoctorApiResult<>(SUCCESS_CODE, SUCCESS_MESSAGE, list);
        }
        return new DoctorApiResult<>(apiResult.getCode(), apiResult.getMessage(), list);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getInfo() {
        return info;
    }

    public void setInfo(T info) {
        this.info = info;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
